##############################################################
#
#
# R script file for the figures in:
#
## "All Crises Are Global: Capital Cycles in an 
## Imbalanced International Political Economy"
#
## By:
#
## Sarah Bauerle Danzman & Thomas Oatley & W. Kindred Winecoff
#
## Published in International Studies Quarterly
#
#
##############################################################


load("~/AmericanHegemonyGlobalCapital.RData")

#
# Number of crises (not used in final version):
#


FinCrises[is.na(FinCrises)] <- 0

png("~/FinCrises.png", width=1200, height=600)
plot(FinCrises$Year, FinCrises$AllCrises, type="l", bty="n", ylab="Number of Financial Crises", xlab="Year")
dev.off()


#
# Figure 1: Bonanzas and Crises over time:
#


Bonanzas[is.na(Bonanzas)] <- 0

Bonanzas1 <- aggregate(Bonanzas$bonanza, by=list(Category=Bonanzas$year), FUN=sum)

pdf("~/BonanzasCrises.pdf", width=12, height=6)
plot(FinCrises$Year[10:40], FinCrises$BankCrises[10:40], type="l", bty="n", ylab="Number of Capital Events", xlab="Year", lty=1, lwd=2)
lines(Bonanzas1$Category, Bonanzas1$x, type="l", lty=3, lwd=2)
legend("topleft", c("Capital Inflow Bonanzas", "Banking Crises"), lty=c(3,1), lwd=2, bty="n")
dev.off()

#
# Referenced in Appendix:
#

cor(FinCrises$BankCrises[10:40], Bonanzas1$x)
cor.test(FinCrises$BankCrises[10:40], Bonanzas1$x)



#
# Figure 3: KA Gini vs US KA (top 10 imbalances):
#

reg1 <- lm(abs(KAdat$fa_gini_10) ~ abs(KAdat$us_fa_gdp))

png("~/USKAvKAGINI-10.png", width=1200, height=600)
plot(abs(KAdat$us_fa_gdp), abs(KAdat$fa_gini_10), type="n", bty="n", xlab="U.S. Capital Account Balance (% GDP)", ylab = "Global Capital Account Gini Coefficient (Top 10 Imbalances)", ylim = c(0,1), xlim = c(-0.00, 0.06), cex.lab=1.25)
text(abs(KAdat$us_fa_gdp), abs(KAdat$fa_gini_10), labels=KAdat$year, cex=2.0)
abline(reg1)
dev.off()


#
# KA Gini vs US KA (top 20 imbalances, in appendix):
#

reg2 <- lm(abs(KAdat$fa_gini_20) ~ abs(KAdat$us_fa_gdp))

png("~/USKAvKAGINI-20.png", width=1200, height=600)
plot(abs(KAdat$us_fa_gdp), abs(KAdat$fa_gini_20), type="n", bty="n", xlab="U.S. Capital Account Balance (% GDP)", ylab = "Global Capital Account Gini Coefficient (Top 20 Imbalances)", ylim = c(0,1), xlim = c(-0.00, 0.06), cex.lab=1.25)
text(abs(KAdat$us_fa_gdp), abs(KAdat$fa_gini_20), labels=KAdat$year, cex=2.0)
abline(reg2)
dev.off()

#
#Referenced in Appendix:
#

cor(KAdat$us_fa_gdp[1:40], KAdat$fa_gini_10[1:40])
cor(KAdat$us_fa_gdp[1:40], KAdat$fa_gini_20[1:40])



#
# CA Gini vs US CA (in appendix): 
#

reg3 <- lm(FinCrises$CAGINI ~ FinCrises$USCA)

png("~/USCAvCAGINI.png", width=1200, height=600)
plot(FinCrises$USCA, FinCrises$CAGINI, type="n", bty="n", xlab="U.S. Current Account Balance (% GDP)", ylab = "Global Current Account Gini", ylim = c(0,1), xlim = c(-0.06, 0.02))
text(FinCrises$USCA, FinCrises$CAGINI, labels=FinCrises$Year)
abline(reg3)
dev.off()



#
#Alt, US CA v global KA Gini:
#

reg4 <- lm(abs(KAdat$fa_gini_10[1:34]) ~ FinCrises$USCA[6:39])

png("~/USCAvKAGINI.png", width=1200, height=600)
plot(FinCrises$USCA[6:39], abs(KAdat$fa_gini_10[1:34]), type="n", bty="n", xlab="U.S. Current Account Balance (% GDP)", ylab = "Global Capital Account Gini Coefficient", ylim = c(0,1), xlim = c(-0.06, 0.02))
text(FinCrises$USCA[6:39], abs(KAdat$fa_gini_10[1:34]), labels=KAdat$year[1:34])
abline(reg4)
dev.off()

cor(FinCrises$USCA[6:39], KAdat$fa_gini_10[1:34])

#
#
#

library(plotrix)

blanknames <- c("","","","","","","","","","","","","","","","","","","","")


#
# Figure 4: KA surplus countries in 1991:
#
pdf("~/1991.pdf")
barp(KA.1991$KA, names.arg=blanknames, ylim=c(0,600), ylab="Largest Capital Account Surpluses (millions USD)", xlab="", main="1991", col="lightgray", cylindrical=TRUE)
text(x=seq(1,20)+0.2, y=20, adj = c(0,0), labels=KA.1991$Country, srt=90)
dev.off()


#
# Figure 5: KA surplus countries in 2002:
#

pdf("~/2002.pdf")
barp(KA.2002$KA/1000, names.arg=blanknames, ylim=c(0,600), ylab="Largest Capital Account Surpluses (billions USD)", xlab="", main="2002", col="lightgray", cylindrical=TRUE)
text(x=seq(1,20)+0.2, y=20, adj = c(0,0), labels=KA.2002$Country, srt=90)
dev.off()




#
# Appendix CA deficit plots, 1991 & 2006:
#

#Option One: Different Scales:

pdf("~/1991a.pdf")
barp(CA.hist$X1991/1000000000, names.arg=blanknames, ylim=c(-30,0), ylab="Largest Current Account Deficits (billions USD)", xlab="", main="1991", col="lightgray", cylindrical=TRUE)
text(x=seq(1,20)+0.4, y=-1, labels=CA.hist$Country1, srt=90, pos=2)
dev.off()


#Option Two: Same Scale:

pdf("~/1991b.pdf")
barp(CA.hist$X1991/1000000000, names.arg=blanknames, ylim=c(-850,0), ylab="Largest Current Account Deficits (billions USD)", xlab="", main="1991", col="lightgray", cylindrical=TRUE)
text(x=seq(1,20)+0.4, y=-10, labels=CA.hist$Country1, srt=90, pos=2)
 dev.off()
 
pdf("~/2006.pdf")
barp(CA.hist$X2006/1000000000, names.arg=blanknames, ylim=c(-850,0), ylab="Largest Current Account Deficits (billions USD)", xlab="", main="2006", col="lightgray", cylindrical=TRUE)
text(x=seq(1,20)+0.4, y=-10, labels=CA.hist$Country2, srt=90, pos=2)
dev.off()



